package pers.cz.common.pub;

import pers.cz.common.pub.Condition;
import pers.cz.common.pub.Field;
import pers.cz.common.pub.Operator;
import pers.cz.common.pub.Query;
import utils.ObjUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: jef-orm
 * @description: 条件查询（用于复杂查询）
 * @author: Cheng Zhi
 * @create: 2023-08-12 16:02
 **/
public class ConditionQuery implements Query {

    private List<Condition> conditions;

    private String otherBindFinalCondition;

    @Override
    public void clearQueryCondition() {
        otherBindFinalCondition = null;
        conditions.clear();
    }

    @Override
    public void addCondition(String columnName, Operator operator, Object value) {

        if (conditions == null) {
            conditions = new ArrayList<>();
        }

        Condition condition = new Condition();
        condition.setColumnName(columnName);
        condition.setOperator(operator);
        condition.setBindVal(value);
        conditions.add(condition);
    }

    @Override
    public void addCondition(Field queryColumn, Operator operator, Object value) {
        this.addCondition(ObjUtils.camel2under(queryColumn.name()), operator, value);
    }

    @Override
    public void addCondition(Field queryColumn, Object value) {
        this.addCondition(ObjUtils.camel2under(queryColumn.name()), Operator.DEFAULT, value);
    }

    @Override
    public void bindOtherFianlCondition(String sql) {
        otherBindFinalCondition = sql;
    }

    public String getOtherBindFinalCondition() {
        return otherBindFinalCondition;
    }

    public List<Condition> getConditions() {
        return conditions;
    }
}
